package com.service.user.impl;

import java.util.List;

import com.dao.UserDao;
import com.entity.User;
import com.entity.UserAddress;
import com.service.user.UserService;
import com.utils.MybatisUtil;
import com.utils.Pager;

public class UserServiceImpl implements UserService{


	@Override
	public User finLoginName(String name) {
		UserDao mapper = MybatisUtil.getMapper(UserDao.class);
		User user = mapper.loginName(name);
		MybatisUtil.closeCommit();
		return user;
	}

	@Override
	public int finAddUser(User user) {
		UserDao mapper = MybatisUtil.getMapper(UserDao.class);
		int row = -1;
		row = mapper.addUser(user);
		MybatisUtil.closeCommit();
		return row;
	}

	@Override
	public int finLoginNameCount(String name) {
		UserDao mapper = MybatisUtil.getMapper(UserDao.class);
		int row = mapper.loginNameCount(name);
		MybatisUtil.closeCommit();
		return row;
	}

	@Override
	public List<UserAddress> finUserAddress(int userid) {
		UserDao mapper = MybatisUtil.getMapper(UserDao.class);
		List<UserAddress> list = mapper.userAddress(userid);
		MybatisUtil.closeCommit();
		return list;
	}

	@Override
	public int finUserAddress(UserAddress add) {
		UserDao mapper = MybatisUtil.getMapper(UserDao.class);
		int row = mapper.addUserAddress(add);
		MybatisUtil.closeCommit();
		return row;
	}

	@Override
	public UserAddress finUserAddressID(int id) {
		UserDao mapper = MybatisUtil.getMapper(UserDao.class);
		UserAddress useradd = mapper.userAddressID(id);
		MybatisUtil.closeCommit();
		return useradd;
	}

	/**
	 * 查询用户总记录数
	 */
	@Override
	public int finUserCount() {
		UserDao mapper = MybatisUtil.getMapper(UserDao.class);
		int row = mapper.userCount();
		MybatisUtil.closeCommit();
		return row;
	}

	/**
	 * 查询所有用户信息
	 */
	@Override
	public List<User> finUserAll(Pager page) {
		UserDao mapper = MybatisUtil.getMapper(UserDao.class);
		List<User> list = mapper.userAll(page);
		MybatisUtil.closeCommit();
		return list;
	}

	/**
	 * 根据用户Id判断该编号是否存在地址信息！
	 * 
	 */
	@Override
	public int finUserByIdAddress(int userId) {
		UserDao mapper = MybatisUtil.getMapper(UserDao.class);
		int row = mapper.userByIdAddress(userId);
		MybatisUtil.closeCommit();
		return row;
	}

	@Override
	public int finRemoveUserById(int id) {
		UserDao mapper = MybatisUtil.getMapper(UserDao.class);
		int row = mapper.removeUserById(id);
		MybatisUtil.closeCommit();
		return row;
	}

	@Override
	public int finModifyUserId(User user) {
		UserDao mapper = MybatisUtil.getMapper(UserDao.class);
		int row = -1;
		if(user.getId() != 0) {
			row = mapper.modifyUpdate(user);
		}else {
			row = mapper.modifyAddUser(user);
		}
		MybatisUtil.closeCommit();
		return row;
	}

	@Override
	public User finUserById(int id) {
		UserDao mapper = MybatisUtil.getMapper(UserDao.class);
		User user = mapper.userById(id);
		MybatisUtil.closeCommit();
		return user;
	}
}

